package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;

public class TimeServlet extends HttpServlet{
	
	public TimeServlet() {
		System.out.println("TimeServlet.TimeServlet()");
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		String maxOnLine = config.getInitParameter("maxOnLine");
		System.out.println("maxOnLine:" + maxOnLine);
	}

	// 访问servlet时候，默认访问的就是service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletConfig servletConfig = getServletConfig();
		String maxOnLine = servletConfig.getInitParameter("maxOnLine");
		System.out.println("maxOnLine:" + maxOnLine);
		
		ServletContext servletContext = getServletContext();
		String pageSize = servletContext.getInitParameter("pageSize");
		System.out.println("pageSize:" + pageSize);
		
		// 告诉浏览器返回的是一个网页
		resp.setContentType("text/html");
		
		Date date = new Date();
		System.out.println(date);
		// Fri Aug 23 11:06:54 CST 2019   "yyyy-MM-dd hh:mm:ss"
		// 2019-08-23 11:08:23
		SimpleDateFormat simpleDateFormat = 
				new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String nowTime = simpleDateFormat.format(date);
		
		// 返回给浏览器
		PrintWriter printWriter = resp.getWriter();
		//printWriter.println("<p>2019-08-23 11:08:23</p>");
		printWriter.println("<p>" + nowTime + "</p>");
		printWriter.close();
	}
	
	
	
	public static void main(String[] args) {
		int result = f(5);
		System.out.println(result);
	}

	// 5!=5*4*3*2*1
	private static int f(int n) {
		int result = 1;
		for (int i = n; i >= 1; i--) {
			result = result * i;
		}
		return result;
	}
	
	// 5!=5*4*3*2*1
	// factorial(5) = 5 * factorial(4);// 5*24=120
	// factorial(4) = 4 * factorial(3);// 4*6=24
	// factorial(3) = 3 * factorial(2);// 3*2=6
	// factorial(2) = 2 * factorial(1);// 2
	// factorial(1) = 1;
	@Test
	public void test11() {
		int result = factorial(5);
		System.out.println(result);
	}

	private int factorial(int n) {
		if (n == 1) {
			return 1;
		}
		
		return n * factorial(n - 1);
	}
	
	
	
}
